package com.vic.exchange.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.vic.utils.RabbitmqUtils;

/**
 */
public class Consumer01 {

    private static final String EXCHANGE_NAME = "exchange_topic";
    private static final String QUEUE_01 = "queue_01";
    private static final String QUEUE_01_ROUTING_KEY = "*.rabbit.*";

    public static void main(String[] argv) throws Exception {
        Channel channel = RabbitmqUtils.getChannel();

        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 声明队列
        channel.queueDeclare(QUEUE_01, true, false, false, null);
        // 将交换机和队列进行绑定
        channel.queueBind(QUEUE_01, EXCHANGE_NAME, QUEUE_01_ROUTING_KEY, null);

        // 消息成功之后的回调
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("接收到消息: " + message);
        };

        // 取消消费者的回调
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("取消消费者时的回调接口");
        };

        // 消费者消费消息
        channel.basicConsume(QUEUE_01, deliverCallback, cancelCallback);

        System.out.println("Consumer01 开始消费，routing key为: " + QUEUE_01_ROUTING_KEY);
    }
}